package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.filter.FilterMultipleAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.eventbus_event.DrawerMultipleCompleteEvent;
import com.wwwarehouse.common.eventbus_event.DrawerMultipleResetEvent;
import com.wwwarehouse.common.fragment.DeskDrawerMultipleFragment;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.FilterMultipleDoubleAdapter;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileListBean;
import com.wwwarehouse.usercenter.eventbus_event.DrawerMultipleResetDoubleEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/8/25.
 * 项目中的抽屉布局(多选)
 */

public class DeskDrawerMultipleDoubleFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private TextView mTitleTxt;
    private GridView mGridView;
    private TextView mTitleTxt2;
    private GridView mGridView2;
    private TextView mTitleTxt3;
    private Button mRestBtn;
    private Button mCompleteBtn;

    private String mTitle;
    private List<BRCustomerFileListBean.ScreeningTagsBean> mFilterList;
    private String mTitle2;
    private List<BRCustomerFileListBean.ScreeningSalesMansBean> mFilterList2;
    private FilterMultipleDoubleAdapter mAdapter;
    private FilterMultipleDoubleAdapter mAdapter2;
    private int mColumns;

    private ArrayList<String> mSelectedPositionSet;
    private ArrayList<String> mSelectedPositionSet2;

    private static final String KEY_TITLE = "KEY_TITLE";
    private static final String KEY_LIST= "KEY_LIST";
    private static final String KEY_TITLE_2 = "KEY_TITLE_2";
    private static final String KEY_LIST_2 = "KEY_LIST_2";
    private static final String KEY_COLUMNS = "KEY_COLUMNS";

    public static final DeskDrawerMultipleDoubleFragment newInstance(String title, List<BRCustomerFileListBean.ScreeningTagsBean> filterList, String title2, List<BRCustomerFileListBean.ScreeningSalesMansBean> filterList2, int columns) {
        DeskDrawerMultipleDoubleFragment deskDrawerMultipleFragment = new DeskDrawerMultipleDoubleFragment();

        Bundle bundle = new Bundle();
        bundle.putString(KEY_TITLE, title);
        bundle.putSerializable(KEY_LIST, (Serializable) filterList);
        bundle.putString(KEY_TITLE_2, title2);
        bundle.putSerializable(KEY_LIST_2, (Serializable) filterList2);
        bundle.putInt(KEY_COLUMNS, columns);

        deskDrawerMultipleFragment.setArguments(bundle);

        return deskDrawerMultipleFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_desk_drawer_multiple_double, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
        initData();
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTitle = bundle.getString(KEY_TITLE);
            mFilterList = (List<BRCustomerFileListBean.ScreeningTagsBean>) bundle.getSerializable(KEY_LIST);
            mTitle2 = bundle.getString(KEY_TITLE_2);
            mFilterList2 = (List<BRCustomerFileListBean.ScreeningSalesMansBean>) bundle.getSerializable(KEY_LIST_2);
            mColumns = bundle.getInt(KEY_COLUMNS);

            mTitleTxt.setText(mTitle);
            mGridView.setNumColumns(mColumns);
            mTitleTxt2.setText(mTitle2);
            mGridView2.setNumColumns(mColumns);
            mTitleTxt3.setVisibility(View.GONE);

            mAdapter = new FilterMultipleDoubleAdapter(mActivity, mFilterList, null);
            mGridView.setAdapter(mAdapter);

            mAdapter2 = new FilterMultipleDoubleAdapter(mActivity, null, mFilterList2);
            mGridView2.setAdapter(mAdapter2);
        }

        mSelectedPositionSet = new ArrayList<>();
        mSelectedPositionSet2 = new ArrayList<>();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mTitleTxt = findView(mRootView, R.id.tv_title);
        mGridView = findView(mRootView, R.id.gv_content);
        mTitleTxt2 = findView(mRootView, R.id.tv_title2);
        mGridView2 = findView(mRootView, R.id.gv_content2);
        mTitleTxt3 = findView(mRootView, R.id.tv_title3);
        mRestBtn = findView(mRootView, R.id.btn_reset);
        mCompleteBtn = findView(mRootView, R.id.btn_complete);

    }

    private void initEvent() {
        mRestBtn.setOnClickListener(this);
        mCompleteBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_reset) {
            mSelectedPositionSet.clear();
            mSelectedPositionSet2.clear();

            for (BRCustomerFileListBean.ScreeningTagsBean filterBean : mFilterList) {
                filterBean.setSelect(false);
            }
            for (BRCustomerFileListBean.ScreeningSalesMansBean filterBean : mFilterList2) {
                filterBean.setSelect(false);
            }

            mAdapter.notifyDataSetChanged();

            EventBus.getDefault().post(new DrawerMultipleResetDoubleEvent(mSelectedPositionSet, mSelectedPositionSet2));

        } else if (id == R.id.btn_complete) {
            mSelectedPositionSet.clear();
            mSelectedPositionSet2.clear();

            for (int i = 0; i < mFilterList.size(); i++) {
                if (mFilterList.get(i).isSelect()) {
                    mSelectedPositionSet.add(mFilterList.get(i).getTagId());
                }
            }

            for (int i = 0; i < mFilterList2.size(); i++) {
                if (mFilterList2.get(i).isSelect()) {
                    mSelectedPositionSet2.add(mFilterList2.get(i).getSalesManId());
                }
            }

            EventBus.getDefault().post(new DrawerMultipleResetDoubleEvent(mSelectedPositionSet, mSelectedPositionSet2));

//            ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
        } else {
        }
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }
}
